<?php
namespace app\admin\controller;
use app\admin\controller\AdminController;
/**
 * 用户管理
 */
class UserController extends AdminController {

    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'menu' => array(
                    array(
                        'name' => '用户列表',
                        'url' => url('index'),
                        'icon' => 'bars',
                    ),
                ),
            );
    }

    /**
     * 列表
     */
    public function index(){
        //筛选条件
        $where = array();
        $keyword = request('request.keyword','');
        $groupId = request('request.group_id','');
        if(!empty($keyword)){
            $where[] = "A.username = :keyword OR A.user_id = :keyword OR A.email = :keyword OR B.name = :keyword";
            $where[':keyword'] = $keyword;
        }
        if(!empty($groupId)){
            $where['B.group_id'] = $groupId;
        }
        //设置目录导航
        $this->adminCrumb[] = array('url'=>url('index'),'name'=>'用户管理');
        //URL参数
        $pageMaps = array();
        $pageMaps['keyword'] = $keyword;
        $pageMaps['group_id'] = $groupId;
        $pageList = target('forum/User')->page(20)->loadList($where,20);
        $this->pager = target('forum/User')->pager;
        $page = $this->getPageShow($pageMaps);
        $this->assign('pageList', $pageList);
        $this->assign('page', $page);
        $this->assign('pageMaps',$pageMaps);
        $this->adminDisplay();
    }

    /**
     * 编辑
     */
    public function edit(){
    	if(!IS_POST){
    		$userId = request('get.user_id','','intval');
            if(empty($userId)){
                $this->error('参数不能为空！');
            }
            $model = target('forum/User');
            $info = $model->getInfo($userId);
            if(!$info){
                $this->error($model->getError());
            }
	    	//设置目录导航
	    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'用户管理');
	    	$this->adminCrumb[] = array('url'=>url('edit',array('user_id'=>$info['user_id'])),'name'=>'用户编辑');
	        $this->assign('groupList', target('forum/UserGroup')->loadList());
	        $this->assign('name','编辑');
	        $this->assign('info',$info);
	        $this->adminDisplay('info');
        }else{
            if(target('forum/User')->saveData('edit')){
                $this->success('用户编辑成功！',url('index'));
            }else{
                $msg = target('forum/User')->getError();
                if(empty($msg)){
                    $this->error('用户编辑失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 删除
     */
    public function del(){
        $userId = request('post.data');
        if(empty($userId)){
            $this->error('参数不能为空！');
        }
        //删除操作
        if(target('forum/User')->delData($userId)){
            $this->success('用户删除成功！');
        }else{
            $msg = target('forum/User')->getError();
            if(empty($msg)){
                $this->error('用户删除失败！');
            }else{
                $this->error($msg);
            }
        }
    }
}

